
<script src="http://jqueryui.com/themeroller/themeswitchertool/"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/ui-lightness/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="colorpicker/jquery.colorpicker.js"></script>
<link href="colorpicker/jquery.colorpicker.css" rel="stylesheet" type="text/css"/>
<script src="timepicker/jquery.ui.timepicker.js"></script>
<link href="timepicker/jquery.ui.timepicker.css" rel="stylesheet" type="text/css"/>


<script>
    $(function() {                    
        $("#sear").autocomplete({
            source: "states.php",
            minLength: 2,
            select: function(event, ui) {
                $('#sear_id').val(ui.item.id);
            }
        });
    });
</script>

<!-- FUNCIONS PER MOSTRAR EL COLORPICKER I PER MOSTRAR EL COLOR COM A BACKGROUND D'UNA DIV-->
<script>
    function recolor() {
        $( function() {
            var elcolor = $("#color").val();
            $('.cp-full').colorpicker({
                showOn: 'focus',
                altField: '#colorM',
                altOnChange: true,
                altProperties: 'background-color'
            });
        
        });
    }
    
</script>
<script>
    function mostraColor() {
        var elcolor = $("#color").val();
        $('#colorM').css('backgroundColor', '#' + elcolor);         
    }
    
</script>

<script type='text/javascript'>

    $(document).ready(function() {
	
        var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();
		
        $('#calendar').fullCalendar({
            editable: true,
            firstDay: 1,
            monthNames:[
                'January', 'February', 'March', 'April', 'May', 'June', 'JulyA',
                'August', 'September', 'October', 'November', 'December'],
            events: [
                {
                    title: 'All Day Event',
                    start: new Date(y, m, 1)
                },
                {
                    title: 'Long Event',
                    start: new Date(y, m, d-5),
                    end: new Date(y, m, d-2)
                },
                {
                    id: 999,
                    title: 'Repeating Event',
                    start: new Date(y, m, d-3, 16, 0),
                    allDay: false
                },
                {
                    id: 999,
                    title: 'Repeating Event',
                    start: new Date(y, m, d+4, 16, 0),
                    allDay: false
                },
                {
                    title: 'Meeting',
                    start: new Date(y, m, d, 10, 30),
                    allDay: false
                },
                {
                    title: 'Lunch',
                    start: new Date(y, m, d, 12, 0),
                    end: new Date(y, m, d, 14, 0),
                    allDay: false
                },
                {
                    title: 'Birthday Party',
                    start: new Date(y, m, d+1, 19, 0),
                    end: new Date(y, m, d+1, 22, 30),
                    allDay: false
                },
                {
                    title: 'Click for Google',
                    start: new Date(y, m, 28),
                    end: new Date(y, m, 29),
                    url: 'http://google.com/'
                }
            ]
        });
		
    });

</script>
<script type='text/javascript'>

    $(document).ready(function() {
	
        var date = new Date();
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();
		
        $('#calendaravui').fullCalendar({
            editable: true,
            defaultView: 'agendaDay',
            firstDay: 1,
            height: 450,
            monthNamesShort: [
                'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                'XXl', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
            ],
            dayNames:['Sunday', 'Monday', 'Tuesday', 'Dimecres',
                'Thursday', 'Friday', 'Saturday'],
            events: [
                {
                    title: 'All Day Event',
                    start: new Date(y, m, 1)
                },
                {
                    title: 'Long Event',
                    start: new Date(y, m, d-5),
                    end: new Date(y, m, d-2)
                },
                {
                    id: 999,
                    title: 'Repeating Event',
                    start: new Date(y, m, d-3, 16, 0),
                    allDay: false
                },
                {
                    id: 999,
                    title: 'Repeating Event',
                    start: new Date(y, m, d+4, 16, 0),
                    allDay: false
                },
                {
                    title: 'Meeting',
                    start: new Date(y, m, d, 10, 30),
                    allDay: false
                },
                {
                    title: 'Lunch',
                    start: new Date(y, m, d, 12, 0),
                    end: new Date(y, m, d, 14, 0),
                    allDay: false
                },
                {
                    title: 'Birthday Party',
                    start: new Date(y, m, d+1, 19, 0),
                    end: new Date(y, m, d+1, 22, 30),
                    allDay: false
                },
                {
                    title: 'Click for Google',
                    start: new Date(y, m, 28),
                    end: new Date(y, m, 29),
                    url: 'http://google.com/'
                }
            ]
        });
		
    });

</script>
<script type='text/javascript'>
    $(document).ready(function() {

        //Cuando el sitio carga...

        $(".tab_content").hide(); //Esconde todo el contenido

        $("ul.tabs li:first").addClass("active").show(); //Activa la primera tab

        $(".tab_content:first").show(); //Muestra el contenido de la primera tab

        //On Click Event

        $("ul.tabs li").click(function() {

            $("ul.tabs li").removeClass("active"); //Elimina las clases activas

            $(this).addClass("active"); //Agrega la clase activa a la tab seleccionada

            $(".tab_content").hide(); //Esconde todo el contenido de la tab

            var activeTab = $(this).find("a").attr("href"); //Encuentra el valor del atributo href para identificar la tab activa + el contenido

            $(activeTab).fadeIn(); //Agrega efecto de transición (fade) en el contenido activo

            return false;

        });

    });   
</script>
<script language="javascript" type="text/javascript">
    $(function() {

        var fixHelper = function(e, ui) {
            ui.children().each(function() {
                $(this).width($(this).width());
            });
            return ui;
        };

        $("#sort tbody").sortable({
            helper: fixHelper
        }).disableSelection();
               
    });
            
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        //$( "#dataInici" ).datepicker();
        $.datepicker.setDefaults( $.datepicker.regional[ "" ] );
        //$( "#dataInici" ).datepicker( $.datepicker.regional[ "es" ] );
        $( "#dataInici" ).datepicker({
            dateFormat: 'dd/mm/yy', 
            changeMonth: true, 
            changeYear: true, 
            yearRange: '-100:+0'} );
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        //$( "#dataInici" ).datepicker();
        $.datepicker.setDefaults( $.datepicker.regional[ "" ] );
        $( "#dataFi" ).datepicker( $.datepicker.regional[ "es" ] );
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDilluns').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDilluns').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDimarts').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDimarts').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDimecres').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDimecres').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDijous').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDijous').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDivendres').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDivendres').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDissabte').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDissabte').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDiumenge').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDiumenge').timepicker();
    });
</script>

<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDilluns2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDilluns2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDimarts2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDimarts2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDimecres2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDimecres2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDijous2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDijous2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDivendres2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDivendres2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDissabte2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDissabte2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaIniciDiumenge2').timepicker();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $('#horaFiDiumenge2').timepicker();
    });
</script>


<script language="javascript" type="text/javascript">
    $(function() {
        $( "#dilluns" ).buttonset();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $( "#dimarts" ).buttonset();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $( "#dimecres" ).buttonset();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $( "#dijous" ).buttonset();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $( "#divendres" ).buttonset();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $( "#dissabte" ).buttonset();
    });
</script>
<script language="javascript" type="text/javascript">
    $(function() {
        $( "#diumenge" ).buttonset();
    });
</script>
<!--    ESQUERRA  -->
<!-- VISIBILITAT AFEGIR POTCREAR -->
<script language="javascript" type="text/javascript">
    $(function() {
        //$("div#missatge").show("scale","slow").delay(3000).hide("blind","slow");
<?php if (isset($_SESSION['potCrear']) && $_SESSION['potCrear'] == "SI") { ?>
            $( "#txt-v-calendari-e-b-afegir" ).show();
<?php } else { ?>
            $( "#txt-v-calendari-e-b-afegir" ).hide();
<?php } ?>
        
    });
</script>

<!-- OMPLIR CALENDARIS -->
<script>
    $(function() { 
        $.ajax({ 
            data: "accio=omplirLlista", 
            type: "GET", 
            dataType: "json", 
            url: "controlador/CalendariControl.php", 
            success: function(data){ 
                mostraLlista(data);
            }
        });
    });    
</script>

<!-- MOSTRA LA LLISTA DE CALENDARIS -->
<script language="javascript" type="text/javascript">
    function mostraLlista(data) {
        $.each(data,function(index,value) {                
            $("#llistaEsquerra").append("<li><a href='#tab1' id='"+data[index].codiCalendari+"'>"+data[index].codiCalendari+"-"+data[index].descripcioCalendari+"</a></li>");          
        });
    }
</script>


<!-- CLIC A AFEGIR CALENDARI -->
<script type='text/javascript'>
    $(function() { 
        $("#txt-v-calendari-e-b-afegir").click(function() {
            $(".tab_content").hide();
            $("#tab1").hide();
            $("#tab1").show();
            $("#tab2").hide();
            $("ul.tabs li").removeClass("active");
            $("ul.tabs li:first").addClass("active").show(); //Activa la primera tab
            $("#fCalendari").reset();
            ompleResponsables();
            recolor();
            $(".tab_content:first").show(); //Muestra el contenido de la primera tab
<?php
$_SESSION['calendari'] = "";
$_SESSION['responsableSelected'] = "";
$_SESSION['pestanyaCalendari'] = "Dades";
?>;
        });
    });
</script>

<!-- CLIC A CERCAR CALENDARI -->
<!--
    S'ESBORRA PRIMER LA LLISTA #llistaEsquerra
    SEGONS EL RADIO #cercaDescripcio $('input:radio[name=cercaDescripcio]:checked').val();
    CERCA PER DESCRIPCIO -> CalendariControl.php / processomplirLlistaDescripcio
    CERCA PER RESPONSABLE -> CalendariControl.php / processomplirLlistaResponsable
-->

<script language="javascript" type="text/javascript">
    $(function() {
        $("#imgSearch").click(function() {
            var selec = $('input:radio[name=cercaDescripcio]:checked').val();
            $("#llistaEsquerra").empty();
            if(selec == "descripcio"){
                var valorCercar = $("#sear1").val();
                $.ajax({ 
                    data: "accio=omplirLlistaDescripcio&valorCercar="+valorCercar, 
                    type: "GET", 
                    dataType: "json", 
                    url: "controlador/CalendariControl.php", 
                    success: function(data){ 
                        mostraLlista(data);
                    }
                });
            }else{
                var valorCercar = $("#sear1").val();
                $.ajax({ 
                    data: "accio=omplirLlistaResponsable&valorCercar="+valorCercar, 
                    type: "GET", 
                    dataType: "json", 
                    url: "controlador/CalendariControl.php", 
                    success: function(data){ 
                        mostraLlista(data);
                    }
                });
            }
        });
    });
</script>



<!-- CLIC A UN CALENDARI DE LA LLISTA PER MOSTRAR EL CALENDARI A LA PESTANYA DADES-->
<script language="javascript" type="text/javascript">
    $(function() {
        $('#llistaEsquerra').delegate('li', 'click', function () {  
            var texto = $(this).text();
            var codiCal = texto.slice(0,texto.indexOf("-"));
            $.ajax({ 
                data: "accio=omplirFCalendari&codiCal="+codiCal, 
                type: "GET", 
                dataType: "json", 
                url: "controlador/CalendariControl.php", 
                success: function(data){ 
                    mostraFCalendari(data);
                }
            });
        });
    });
</script>
<!-- MOSTRA DADES DEL FORMULARI CALENDARI -->
<script language="javascript" type="text/javascript">
    function mostraFCalendari(data) {
        $.each(data,function(index,value) { 
            $("#codiCalendari").val(data[index].codiCalendari);
            $("#descripcioCalendari").val(data[index].descripcioCalendari);
            ompleResponsables(); 
            $("#color").val(data[index].color);
            $('#colorM').css('backgroundColor', '#' + data[index].color);
            recolor();
            
            $("#dataInici").val(data[index].dataInici);
            $("#dataFi").val(data[index].dataFi);
            var $elselect = $('input:radio[name=dilluns]');
            if(data[index].dilluns == 1){
                $elselect.filter('[value=on]').attr('checked',true);   
                $elselect.filter('[value=off]').attr('checked',false);
            }else{
                $elselect.filter('[value=off]').attr('checked',true);   
                $elselect.filter('[value=on]').attr('checked',false);
            }
            $('#dilluns').buttonset();  
            $('#horaIniciDilluns').val(data[index].horaIniciDilluns);
            $('#horaFiDilluns').val(data[index].horaFiDilluns);
            $('#horaIniciDilluns2').val(data[index].horaIniciDilluns2);
            $('#horaFiDilluns2').val(data[index].horaFiDilluns2);
            var $elselect = $('input:radio[name=dimarts]');
            if(data[index].dimarts == 1){
                $elselect.filter('[value=on]').attr('checked',true);   
                $elselect.filter('[value=off]').attr('checked',false);
            }else{
                $elselect.filter('[value=off]').attr('checked',true);   
                $elselect.filter('[value=on]').attr('checked',false);
            }
            $('#dimarts').buttonset();  
            $('#horaIniciDimarts').val(data[index].horaIniciDimarts);
            $('#horaFiDimarts').val(data[index].horaFiDimarts);
            $('#horaIniciDimarts2').val(data[index].horaIniciDimarts2);
            $('#horaFiDimarts2').val(data[index].horaFiDimarts2);
            var $elselect = $('input:radio[name=dimecres]');
            if(data[index].dimecres == 1){
                $elselect.filter('[value=on]').attr('checked',true);   
                $elselect.filter('[value=off]').attr('checked',false);
            }else{
                $elselect.filter('[value=off]').attr('checked',true);   
                $elselect.filter('[value=on]').attr('checked',false);
            }
            $('#dimecres').buttonset();
            $('#horaIniciDimecres').val(data[index].horaIniciDimecres);
            $('#horafiDimecres').val(data[index].horafiDimecres);
            $('#horaIniciDimecres2').val(data[index].horaIniciDimecres2);
            $('#horaFidimecres2').val(data[index].horaFidimecres2);
            var $elselect = $('input:radio[name=dijous]');
            if(data[index].dijous == 1){
                $elselect.filter('[value=on]').attr('checked',true);   
                $elselect.filter('[value=off]').attr('checked',false);
            }else{
                $elselect.filter('[value=off]').attr('checked',true);   
                $elselect.filter('[value=on]').attr('checked',false);
            }
            $('#dijous').buttonset();
            $('#horaIniciDijous').val(data[index].horaIniciDijous);
            $('#horaFiDijous').val(data[index].horaFiDijous);
            $('#horaIniciDijous2').val(data[index].horaIniciDijous2);
            $('#horafiDijous2').val(data[index].horafiDijous2);
            var $elselect = $('input:radio[name=divendres]');
            if(data[index].divendres == 1){
                $elselect.filter('[value=on]').attr('checked',true);   
                $elselect.filter('[value=off]').attr('checked',false);
            }else{
                $elselect.filter('[value=off]').attr('checked',true);   
                $elselect.filter('[value=on]').attr('checked',false);
            }
            $('#divendres').buttonset();
            $('#horaIniciDivendres').val(data[index].horaIniciDivendres);
            $('#horaFiDivendres').val(data[index].horaFiDivendres);
            $('#horaIniciDivendres2').val(data[index].horaIniciDivendres2);
            $('#horaFiDivendres2').val(data[index].horaFiDivendres2);
            var $elselect = $('input:radio[name=dissabte]');
            if(data[index].dissabte == 1){
                $elselect.filter('[value=on]').attr('checked',true);   
                $elselect.filter('[value=off]').attr('checked',false);
            }else{
                $elselect.filter('[value=off]').attr('checked',true);   
                $elselect.filter('[value=on]').attr('checked',false);
            }
            $('#dissabte').buttonset();
            $('#horaIniciDissabte').val(data[index].horaIniciDissabte);
            $('#horaFiDissabte').val(data[index].horaFiDissabte);
            $('#horaIniciDissabte2').val(data[index].horaIniciDissabte2);
            $('#horaFiDissabte2').val(data[index].horaFiDissabte2);
            var $elselect = $('input:radio[name=diumenge]');
            if(data[index].diumenge == 1){
                $elselect.filter('[value=on]').attr('checked',true);   
                $elselect.filter('[value=off]').attr('checked',false);
            }else{
                $elselect.filter('[value=off]').attr('checked',true);   
                $elselect.filter('[value=on]').attr('checked',false);
            }
            $('#diumenge').buttonset();
            $('#horaIniciDiumenge').val(data[index].horaIniciDiumenge);
            $('#horaFiDiumenge').val(data[index].horaFiDiumenge);
            $('#horaIniciDiumenge2').val(data[index].horaIniciDiumenge2);
            $('#horaFiDiumenge2').val(data[index].horaFiDiumenge2);

        });
    }
</script>

<!-- FUNCIÓ QUE OMPLE I MOSTRA ELS RESPONSABLES AL FORMULARI DE CALENDARI-->
<script>
    function mostraResponsables(data) {
        $.each(data,function(index,value) {
            $("#responsable").append("<option value='"+data[index].codiResponsable+"' "+data[index].responsableSelected+">"+data[index].cognom1+" "+data[index].cognom2+", "+data[index].nom+"</option>");
        });
    }
    function ompleResponsables() {
        $(function() { 
            $("#responsable").empty();
            $.ajax({ 
                data: "accio=omplirSelect", 
                type: "GET", 
                dataType: "json", 
                url: "./controlador/ResponsableControl.php", 
                success: function(data){ 
                    mostraResponsables(data);
                }
            });
        }); 
    }
   
    
</script>